package com.visikard.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.visikard.application.UIMessage;
import com.visikard.application.VKRegApplication;
import com.visikard.asynctask.BaseTask;
import com.visikard.asynctask.LogoutTask;
import com.visikard.item.CouponBuilderItem;
import com.visikard.ui.R;
import com.visikard.ui.fragment.ListCouponFragment;
import com.visikard.ui.fragment.ListCouponsRTUIFragment;
import com.visikard.ui.fragment.MerchantHomeFragment;
import com.visikard.ui.fragment.MerchantNavigationFragment;
import com.visikard.ui.fragment.MerchantNavigationFragment.IMerchantNaviFragment;
import com.visikard.ui.fragment.RedeemNowFragment;
import com.visikard.utility.BitmapUtils;
import com.visikard.utility.UILImageUtils;

public class MerchantHomeActivity extends BaseSlidingFragmentActivity implements IMerchantNaviFragment, OnClickListener {

	private ImageView imgMenu, imgAdd, imgAvatar;
	private TextView txtTitle;

	MerchantNavigationFragment mLeftNavigationFragment;

	@Override
	public void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		/*
		 * relaunch app in case launch from notification while app were killed
		 */
		if (VKRegApplication.uid == -1 || VKRegApplication.merchantAccountObj == null) {
			navigateTo(LauncherActivity.class);
			finish();
			return;
		}

		setContentView(R.layout.merchant_home_activity);

		imgMenu = (ImageView) findViewById(R.id.home_menu_slide);
		imgAdd = (ImageView) findViewById(R.id.home_menu_points);
		imgAvatar = (ImageView) findViewById(R.id.image_avatar);
		txtTitle = (TextView) findViewById(R.id.txt_title);

		imgMenu.setOnClickListener(this);
		imgAdd.setOnClickListener(this);

		if (mLeftNavigationFragment == null) {
			mLeftNavigationFragment = new MerchantNavigationFragment();
			mLeftNavigationFragment.setListener(this);
			setLeftMenuContent(mLeftNavigationFragment);
		}

		// setHomeContent(new MerchantHomeFragment(), false, SlidingMenu.TOUCHMODE_MARGIN);
		setHomeContent(new RedeemNowFragment(), false, SlidingMenu.TOUCHMODE_MARGIN);

		// downloading avatar
		int logoSize = getMerchantLogoSize();
		UILImageUtils.loadImage(imgAvatar, // image view
				VKRegApplication.merchantAccountObj.getMerchantLogo(), // url
				logoSize, // size W of view
				logoSize, // size H of view
				BitmapUtils.CIRCLE, // round all
				0, // round size
				0, // shadow
				0, // shadow resource
				0, 0);

		// merchant name
		txtTitle.setText(VKRegApplication.merchantAccountObj.getMerchantName());
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			showExitDialog();
			return true;
		}
		return false;
	}

	public void showExitDialog() {
		AlertDialog dialog = new AlertDialog.Builder(this).setTitle(R.string.main_screen_dlg_exit_title)
				.setMessage(R.string.main_screen_dlg_exit_msg)
				.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				}).setNegativeButton(getString(android.R.string.no), null).create();
		dialog.show();
	}

	public void showConfirmDeleteDialog(final IDeleteCoupon mCallback, final CouponBuilderItem item) {
		AlertDialog dialog = new AlertDialog.Builder(this).setTitle(R.string.alert_delete_coupon_confirm_title)
				.setMessage(R.string.alert_delete_coupon_warnning)
				.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						mCallback.onDeleteCoupon(item);
					}
				}).setNegativeButton(getString(android.R.string.no), null).create();
		dialog.show();
	}

	public interface IDeleteCoupon {
		public void onDeleteCoupon(CouponBuilderItem item);

	}

	@Override
	public void doLogOut() {
		AlertDialog alerDialod = new AlertDialog.Builder(this).setTitle(R.string.logout_btn_logout)
				.setMessage(R.string.logout_title_logout)
				.setPositiveButton(R.string.logout_btn_logout, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						BaseTask mAsyncTask = new LogoutTask(mHandler);
						mAsyncTask.execute(VKRegApplication.uid, VKRegApplication.loginSession);
					}
				}).setNegativeButton(getString(R.string.literal_cancel), null).create();
		alerDialod.show();
	}

	private int getMerchantLogoSize() {
		float sizeDp = getResources().getDimension(R.dimen.dimen_70_140);
		float sizePx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sizeDp, getResources()
				.getDisplayMetrics());
		return (int) sizePx;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.home_menu_slide:
				openLeftMenu();
				return;
			case R.id.home_menu_points:
				doLogOut();
				return;
		}
		super.onClick(v);
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
			case UIMessage.MSG_LOGOUT:
				((VKRegApplication) getApplication()).logout();
				dismissProgressDialog();
				Intent intent;
				intent = new Intent(MerchantHomeActivity.this, MerchantLoginActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
				finish();
				overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out);
				return true;
		}
		return super.handleMessage(msg);
	}

	@Override
	public void doCouponBuilder() {
		// TODO Auto-generated method stub

		setHomeContent(new ListCouponFragment(), false, SlidingMenu.TOUCHMODE_MARGIN);
	}

	@Override
	public void doRedeemNow() {
		setHomeContent(new RedeemNowFragment(), false, SlidingMenu.TOUCHMODE_MARGIN);
	}

	@Override
	public void doHome() {
		setHomeContent(new MerchantHomeFragment(), false, SlidingMenu.TOUCHMODE_MARGIN);
	}

	@Override
	public void doRTUICoupon() {
		setHomeContent(new ListCouponsRTUIFragment(), false, SlidingMenu.TOUCHMODE_MARGIN);
	}

}
